﻿using System;
using System.Collections.Generic;
using IntelligentStorWpfApp.Interface;
using IntelligentStorWpfApp.Services;
using IntelligentStorWpfApp.Model;
using System.Data;
using log4net;
using IntelligentStorWpfApp.Helper;

namespace IntelligentStorWpfApp.Controller
{
    public class LinkProductPositionController
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(LinkProductPositionController));
        LinkProductPositionInterface lppInterface = new LinkProductPositionServices();
        StatusInterface statusInterface = new StatusServices();
        /// <summary>
        /// add draft table
        /// </summary>
        /// <param name="listLinkpp"></param>
        /// <returns></returns>
        public bool addDraftLink(List<LinkProductPosition> listLinkpp)
        {
            bool result=false;
            int i;
            try
            {
                for (i = 0; i < listLinkpp.Count; i++)
                {
                    result= lppInterface.addLinkDraft(listLinkpp[i]);
                    LogHelper.AddLog("ProductId:" + listLinkpp[i].ProductId + " Action:" + listLinkpp[i].Action + " SetTime:" + listLinkpp[i].SetTime.ToString());
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
                LogHelper.AddLog(ex.ToString());
                return false;
                
            }
            return result;
        }
        /// <summary>
        /// delete draft
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool deleteDraftLink(int Id)
        {
            return lppInterface.deleteLinkDraft(Id) ;
        }
        /// <summary>
        /// draft table
        /// </summary>
        /// <returns></returns>
        public List<LinkProductPosition> getAllDraftLinks()
        {
            List<LinkProductPosition> listPposition = new List<LinkProductPosition>();
            try
            {
                DataTable dt = lppInterface.getAllLinksDraft();
                LinkProductPosition linkpp = new LinkProductPosition();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    linkpp.ID = Convert.ToInt32(dt.Rows[i]["ID"]);
                    linkpp.ProductId = dt.Rows[i]["ProductId"].ToString();
                    linkpp.PositionId = Convert.ToInt32(dt.Rows[i]["PositionId"]);
                    linkpp.SetTime = Convert.ToDateTime(dt.Rows[i]["SetTime"]);
                    linkpp.Action = dt.Rows[i]["Action"].ToString();
                    listPposition.Add(linkpp);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
            }

            return listPposition;
        }
        /// <summary>
        /// draft link by productId
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public LinkProductPosition GetDraftLinkByProductId(string  productId)
        {
            LinkProductPosition lpp = null;
            try
            {
                DataTable dt = lppInterface.GetDraftLinkByProductId(productId);
                if (dt.Rows.Count > 0)
                {
                    lpp = new LinkProductPosition();
                    lpp.ID = Convert.ToInt32(dt.Rows[0]["ID"]);
                    lpp.ProductId = productId;
                    lpp.PositionId = int.Parse(dt.Rows[0]["PositionId"].ToString());
                    lpp.SetTime = Convert.ToDateTime(dt.Rows[0]["SetTime"]);
                    lpp.Action = dt.Rows[0]["Action"].ToString();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
            }
            return lpp;
        }

        /// <summary>
        /// get draft link by position
        /// </summary>
        /// <param name="pId"></param>
        /// <returns></returns>
        public LinkProductPosition GetDraftLinkByPostionId(int pId)
        {
            LinkProductPosition lpp = null;
            try
            {
                DataTable dt = lppInterface.GetDraftLinkByPositionId(pId);
                if (dt.Rows.Count > 0)
                {
                    lpp = new LinkProductPosition();
                    lpp.ID = Convert.ToInt32(dt.Rows[0]["ID"]);
                    lpp.ProductId = dt.Rows[0]["ProductId"].ToString();
                    lpp.PositionId = pId;
                    lpp.SetTime = Convert.ToDateTime(dt.Rows[0]["SetTime"]);
                    lpp.Action = dt.Rows[0]["Action"].ToString();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
            }
            return lpp;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pId"></param>
        /// <returns></returns>
        public bool DeleteDraftLinkByPositionId(int pId)
        {
            bool result= lppInterface.deleteLinkDraftByPosition(pId);
            if (result)
                LogHelper.AddLog(" PostionId: " + pId.ToString() + " Action:delete, success");
            else
                LogHelper.AddLog(" PostionId: " + pId.ToString() + " Action:delete ,fail");

            return result;
        }

        public bool updateLink(LinkProductPosition linkpp)
        {
            return true;
        }

        public bool deleteLinkByProduct(string productId)
        {
            LinkProductPosition linkPp = new LinkProductPosition();
            try
            {
                linkPp = lppInterface.getLinkByProductId(productId);

                if (lppInterface.deleteLinkByProduct(productId))
                {
                    Status status = new Status();
                    status.PositionId = linkPp.PositionId;
                    status.StoreStatus = 0;
                    statusInterface.UpdateStatus(status);
                    LogHelper.AddLog("产品:" + productId + " 删除位置绑定");
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
                LogHelper.AddLog(ex.ToString());
                return false;
            }
        }

        public bool deleteLinkByPosition(int pId)
        {
            return lppInterface.deleteLinkByPosition(pId);
        }


        public LinkProductPosition getLinkByProductId(string ProductId)
        {
            return lppInterface.getLinkByProductId(ProductId);
        }

        public LinkProductPosition getLinkByPositionId(int pId)
        {
            LinkProductPosition linkPp = null;
            try
            {
                DataTable dt = lppInterface.GetLinkByPositionId(pId);
                if (dt.Rows.Count > 0)
                {
                    linkPp = new LinkProductPosition();
                    linkPp.ProductId = dt.Rows[0]["ProductId"].ToString();
                    linkPp.PositionId = Convert.ToInt32(dt.Rows[0]["PositionId"]);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
            }
            return linkPp;
        }
        public bool linkProductPosition(string productId, int positionId)
        {
            bool result = lppInterface.linkProductPosition(productId, positionId);
            if (result)
                LogHelper.AddLog(" 产品 " + productId + " 绑定位置 " + positionId.ToString() + " success");
            else
                LogHelper.AddLog(" 产品 " + productId + " 绑定位置 " + positionId.ToString() + " fail");

            return result ;
        }
    }
}
